import { BasicColumn, FormSchema } from '/@/components/Table';
import { render } from '/@/utils/common/renderUtils';
import { JCronValidator } from '/@/components/Form';

export const columns: BasicColumn[] = [
  {
    title: '任务名称',
    dataIndex: 'jobName',
    width: 200,
    align: 'left',
  },
  {
    title: '任务分组',
    dataIndex: 'jobGroup',
    width: 150,
    align: 'left',
  },
  {
    title: '任务描述',
    dataIndex: 'description',
    width: 200,
    align: 'left',
  },
  {
    title: '调用目标字符串',
    dataIndex: 'invokeTarget',
    width: 200,
    align: 'left',
  },
  {
    title: '周期表达式',
    dataIndex: 'cronExpression',
    width: 120,
    align: 'left',
  },
  {
    title: '上次运行时间',
    dataIndex: 'prevFireTime',
    width: 180,
  },
  {
    title: '下次运行时间',
    dataIndex: 'nextFireTime',
    width: 180,
  },
  {
    title: '状态',
    dataIndex: 'jobStatus',
    width: 100,
    customRender: ({ text }) => {
      const color = text == '0' ? 'green' : text == '1' ? 'red' : 'gray';
      return render.renderTag(render.renderDict(text, 'quartz_status'), color);
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'jobName',
    label: '任务名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'invokeTarget',
    label: '方法名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'jobStatus',
    label: '任务状态',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'quartz_status',
      stringToNumber: true,
    },
    colProps: { span: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'jobName',
    label: '任务类名',
    component: 'Input',
    required: true,
  },
  {
    field: 'jobGroup',
    label: '任务分组',
    component: 'JDictSelectTag',
    componentProps: () => {
      return {
        dictCode: 'quartz_job_group',
      };
    },
    required: true,
  },
  {
    field: 'description',
    label: '任务描述',
    component: 'Input',
    required: true,
  },
  {
    field: 'invokeTarget',
    label: '调用目标串',
    component: 'Input',
    required: true,
    helpMessage: [
      "Bean调用：Bean名.方法名(参数1, 参数2, 参数n...) 示例：testDataService.executeTestTask(userService, 1, 2L, 3F, 4D, 'abc')",
      "静态类调用：静态类全名.方法名(参数1, 参数2, 参数n...)	示例： com.jeesite.common.config.Global.getConfig('productName')",
      '参数说明：支持Bean名参数、数值类型参数、字符串参数（字符串参数需要加单引号，参数中包含逗号的使用 &amp;#44; 字符转码）',
    ],
  },
  {
    field: 'cronExpression',
    label: 'Cron表达式',
    component: 'JEasyCron',
    defaultValue: '* * * * * ? *',
    rules: [{ required: true, message: '请输入Cron表达式' }, { validator: JCronValidator }],
  },
  {
    field: 'misfirePolicy',
    label: '计划错误策略',
    component: 'JDictSelectTag',
    defaultValue: '0',
    required: true,
    componentProps: {
      type: 'radio',
      dictCode: 'quartz_job_misfire_policy',
      placeholder: '请选择计划错误策略',
      stringToNumber: false,
    },
  },
  {
    field: 'concurrent',
    label: '是否并发执行',
    component: 'JDictSelectTag',
    defaultValue: '0',
    required: true,
    componentProps: {
      type: 'radio',
      dictCode: 'quartz_job_concurrent',
      placeholder: '请选择是否并发执行',
      stringToNumber: false,
    },
  },
  {
    field: 'remarks',
    label: '备注',
    component: 'InputTextArea',
  },
];

export const searchLogFormSchema: FormSchema[] = [
  {
    field: 'jobName',
    label: '任务名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'jobGroup',
    label: '任务分组',
    component: 'JDictSelectTag',
    componentProps: () => {
      return {
        dictCode: 'quartz_job_group',
      };
    },
    colProps: { span: 6 },
  },
  {
    field: 'jobResult',
    label: '是否异常',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'quartz_exeute_result',
      stringToNumber: true,
    },
    colProps: { span: 6 },
  },
];

export const logColumns: BasicColumn[] = [
  {
    title: '日志编码',
    dataIndex: 'id',
    width: 120,
    align: 'left',
  },
  {
    title: '任务名称',
    dataIndex: 'jobName',
    width: 150,
    align: 'left',
  },
  {
    title: '任务分组',
    dataIndex: 'jobGroup',
    width: 100,
    align: 'left',
  },
  {
    title: '调用目标字符串',
    dataIndex: 'invokeTarget',
    width: 180,
    align: 'left',
  },
  {
    title: '日志信息',
    dataIndex: 'jobMessage',
    width: 200,
    align: 'left',
  },
  {
    title: '是否异常',
    dataIndex: 'jobResult',
    width: 60,
    customRender: ({ text }) => {
      const color = text == '0' ? 'green' : text == '1' ? 'red' : 'gray';
      return render.renderTag(render.renderDict(text, 'quartz_exeute_result'), color);
    },
  },
  {
    title: '发生时间',
    dataIndex: 'createTime',
    width: 100,
  },
];

export const formDescSchema = [
  {
    field: 'jobName',
    label: '任务名称',
    labelStyle: {
      width: '100px',
    },
  },
  {
    field: 'jobGroup',
    label: '任务组名',
  },
  {
    field: 'invokeTarget',
    label: '调用目标字符串',
  },
  {
    field: 'jobMessage',
    label: '日志信息',
  },
  {
    field: 'createTime',
    label: '发生时间',
  },
  {
    field: 'jobResult',
    label: '执行状态',
    render: (val, data) => {
      const color = val == '0' ? 'green' : val == '1' ? 'red' : 'gray';
      return render.renderTag(render.renderDict(val, 'quartz_exeute_result'), color);
    },
  },
  {
    field: 'exceptionInfo',
    label: '异常信息',
  },
];
